package com.gxa.modules.service.accessservice.impl;

import com.gxa.common.R;
import com.gxa.modules.mapper.accessmapper.AlarmRulesMapper;
import com.gxa.modules.mapper.accessmapper.OrganizationalMapper;
import com.gxa.modules.pojo.AlarmRules;
import com.gxa.modules.pojo.Organizational;
import com.gxa.modules.service.accessservice.OrganizationalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 86156
 */
@Service
public class OrganizationalServiceImpl implements OrganizationalService {

    @Autowired
    private OrganizationalMapper organizationalMapper;

    @Override
    public R selectOrganizational(String organizationalName, String organizationalLevel,String page,String size) {

        R<Object> r = new R<>();

        int statrIndex=(Integer.parseInt(page)-1)*Integer.parseInt(size);
        int endIndex=Integer.parseInt(page)*Integer.parseInt(size);

        r.setCode(200);
        r.setMsg("查询成功！");
        List<Organizational> list;
        if (organizationalLevel != null || "".equals(organizationalLevel)
                || organizationalName != null || "".equals(organizationalName)){
            list=organizationalMapper.selectOrganizational(organizationalName, organizationalLevel,statrIndex,endIndex);
        }else {
            list=organizationalMapper.selectOrganizational(organizationalName, "1",statrIndex,endIndex);

            for (int i = 0; i < list.size(); i++) {
                long pids=list.get(i).getId();
                list.get(i).setNextPids(organizationalMapper.selectOrganizationalByPids(pids));
            }

        }
        r.setData(list);
        return r;
    }

    @Override
    public R insertOrganizational(Organizational organizational) {
        R<Object> r = new R<>();

        r.setCode(500);
        r.setMsg("新增失败！");

        if (organizationalMapper.insertOrganizational(organizational)>0){
            r.setCode(200);
            r.setMsg("新增成功！");
        }
        return r;
    }

    @Override
    public R deleteOrganizational(String organizationalId) {
        R<Object> r = new R<>();

        if ( organizationalId ==null || "".equals(organizationalId) ){
            r.setCode(500);
            r.setMsg("请选择要删除的信息！");
            return r;
        }

        r.setCode(500);
        r.setMsg("删除失败！");

        String[] strings = organizationalId.split(",");
        for (int i=0;i<strings.length;i++) {
            organizationalId=strings[i];
            if(organizationalMapper.deleteOrganizational(organizationalId)>0){
                r.setCode(200);
                r.setMsg("删除成功！");
            }
        }



        return r;
    }

    @Override
    public R updateOrganizational(Organizational organizational) {
        R<Object> r = new R<>();

        r.setCode(500);
        r.setMsg("修改失败！");

        if (organizationalMapper.updateOrganizational(organizational)>0){
            r.setCode(200);
            r.setMsg("修改成功！");
        }
        return r;
    }
}
